package service

import (
	"github.com/casbin/casbin/v2"
	"github.com/go-redis/redis/v7"
	"github.com/robfig/cron/v3"
	"go.uber.org/zap"
	"golang.org/x/sync/singleflight"
	"gorm.io/gorm"
)

var (
	// Log 日志
	Log  *zap.Logger
	Conf *Config

	// DB 数据库
	DB *gorm.DB

	// Redis KV数据库
	Redis *redis.Client

	// Cache 缓存
	Cache *CacheI

	// SG 单次执行器
	SG *singleflight.Group

	// RBAC 权限
	RBAC *casbin.Enforcer

	// Cron 定时任务
	Cron *cron.Cron
)

type Service struct {
	Cf    *Config
	Db    *gorm.DB
	Redis *redis.Client
	Cache *CacheI
	RBAC  *casbin.Enforcer
	Cron  *cron.Cron
}

func Init() {
	Conf = initConf()
	Log = initLog()
	DB = initDB()
	Redis = initRedis()
	Cache = initCache()
	SG = &singleflight.Group{}
	RBAC = initRBAC(DB)
	Cron = initCron()
}

func initCron() *cron.Cron {
	c := cron.New()

	// 初始化

	return c
}

func New() *Service {
	return &Service{
		Cf:    Conf,
		Db:    DB,
		Redis: Redis,
		Cache: Cache,
		RBAC:  RBAC,
		Cron:  Cron,
	}
}
